package com.jerome.election.server.paxos;

import com.jerome.election.http.HttpConstant;

/**
 * Paxos 消息体
 * @author Jerome
 * @date 2023/10/19
 */
public class PaxosMessage {
    /**
     * @see com.jerome.election.server.MemberStateEnum
     */
    private String method;
    /**
     * 提案编号
     */
    private Integer proposalNumber;
    /**
     * 提案值
     */
    private String proposalValue;

    public PaxosMessage(String method, Integer proposalNumber, String proposalValue) {
        this.method = method;
        this.proposalNumber = proposalNumber;
        this.proposalValue = proposalValue;
    }
    public PaxosMessage(String line) {
        String[] split = line.split(HttpConstant.SPACE);
        this.method = split[0];
        this.proposalNumber = Integer.parseInt(split[1]);
        if (split.length>2){
            this.proposalValue = split[2];
        }
    }

    public String getMethod() {
        return method;
    }

    public Integer getProposalNumber() {
        return proposalNumber;
    }

    public String getProposalValue() {
        return proposalValue;
    }

    /**
     * 构建paxos消息
     */
    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(method).append(HttpConstant.SPACE).append(proposalNumber).append(HttpConstant.SPACE)
                .append(proposalValue);
        return stringBuilder.toString();
    }
}
